
let { version } = require('./package.json');
version = version.replace(/\./g, '_');

module.exports = {
  // 这样表示相对路径， 可以部署在任意路径下，如果为 / 则只能在nginx的html的根路径下面，如果指定为/app/ ，则可以部署在/app下面，默认为/
  publicPath: '/',
  // 静态资源目录 (js, css, img, fonts)
  assetsDir: 'assets',
  //关闭生成map文件
  productionSourceMap: false,
  //指定打包目录
  outputDir: process.env.outputDir,
  //远程访问配置
  devServer: {
    disableHostCheck: false,
  },
  lintOnSave: false,
  //webpack配置
  configureWebpack: (config) => {
    // 打包体积优化
    if (process.env.VUE_APP_ENV === 'production') {
      // 为生产环境修改配置...
      config.mode = 'production'
      //去除打印日志
      config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
      // 将每个依赖包打包成单独的js文件
      config.optimization.runtimeChunk = 'single'
      config.optimization.splitChunks = {
        chunks: 'all',
        maxInitialRequests: Infinity,
        minSize: 20000, // 依赖包超过20000bit将被单独打包
        cacheGroups: {
          vendor: {
            test: /[\\/]node_modules[\\/]/,
            name(module) {
              // get the name. E.g. node_modules/packageName/not/this/part.js
              // or node_modules/packageName
              const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
              // npm package names are URL-safe, but some servers don't like @ symbols
              return `npm.${packageName.replace('@', '')}`
            }
          }
        }
      }
      // 配置打包js增加版本号，避免缓存导致显示不更新
      config.output = {
        ...config.output,
        filename: `assets/js/[name].[chunkhash].${version}.js`,
        chunkFilename: `assets/js/[name].[chunkhash].${version}.js`
      }
    } else {
      // 为开发环境修改配置...
      config.mode = 'development'
    }
  }
}


